package fzrj.qkl.demo.controller;

import fzrj.qkl.demo.model.CommonResponse;
import fzrj.qkl.demo.service.backend.impl.ICarrierBackendService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.lang.Exception;

@RestController
@RequestMapping("/carrier")
public class CarrierController {

    @Autowired
    private ICarrierBackendService carrierBackendService;

    // 注册
    @PostMapping(value = "/add")
    public CommonResponse addCarrier(@RequestParam("name") String name, @RequestParam("info") String info) {
        boolean ok = carrierBackendService.addCarrier(name, info);
        if (ok) {
            return CommonResponse.ok(ok);
        }
        return CommonResponse.fail("1", new Exception("error"));
    }

    // 验证
    @PostMapping(value = "/check")
    public CommonResponse checkCarrier(@RequestParam("id") int id) throws ContractException {
        boolean ok = carrierBackendService.checkUser(id);
        if (ok) {
            return CommonResponse.ok(ok);
        }
        return CommonResponse.fail("1", new Exception("error"));
    }

    // 查询可操作的物流信息列表
    @PostMapping(value = "/getShipments")
    public CommonResponse getShipmentsByCarrier(@RequestParam("id") int id) throws ContractException {
        BigInteger[] shipments = carrierBackendService.getShipmentsByCarrier(id);
        if (shipments != null) {
            return CommonResponse.ok(shipments);
        }
        return CommonResponse.fail("1", new Exception("error"));
    }
}
